/*
 * @Author: xiaohao
 * @Date: 2025-11-04 06:52:06
 * @LastEditors: xiaohao
 * @LastEditTime: 2025-11-05 21:47:40
 * @FilePath: /Project/ConcurrentMemoryPool/include/ConcurrentMemoryPool/PageCache.h
 * @Description:
 */
#pragma once

#include "Common.h"
#include "ObjectPool.h"

class PageCache
{
public:
    static PageCache &GetPageCacheInstance();

    // 将对象映射到span
    Span *MapObjectToSpan(void *obj); 

    // 释放空闲span回到PageCache, 合并相邻的span
    void ReleaseSpanToPageCache(Span *span);

    // 获取一个k页的span
    Span *NewSpan(size_t k);

private:
    PageCache() = default;
    ~PageCache() = default;

    PageCache(const PageCache &) = delete;
    PageCache &operator=(const PageCache &) = delete;

private:
    SpanList _spanLists[NPAGES]; // 页的列表
    ObjectPool<Span> _spanPool; // span的对象池
    std::unordered_map<PAGE_ID, Span *> _idSpanMap; // 页的id和span的映射

public:
    std::mutex _pageMutex;       // 互斥锁
};